/*
 * @Author: Superficial
 * @Date: 2021-08-25 12:36:45
 * @LastEditTime: 2021-10-21 20:22:03
 * @Description:
 */

import { reactive, readonly } from 'vue'
import type { Auth } from '@/constants/auth'
import { getAdminInfo } from '@/store/auth'
import { useLoading } from '@/handcer'

const loading = useLoading()

const DEFAULT_ADMIN_INFO = Object.freeze({
  name: '管理员',
  slogan: '博客管理后台',
  gravatar: '/images/profile/logo-smooth.png',
})

const data = reactive<Auth>({ ...DEFAULT_ADMIN_INFO })
const fetch = () => {
  return loading.promise(getAdminInfo()).then((result) => {
    if (result) {
      Object.keys(result).forEach((key) => {
        // @ts-ignore
        data[key] = result[key]
      })
    }
    return result
  })
}

export const adminState = {
  data: readonly(data),
  loading: readonly(loading.state),
  refresh: fetch,
}

export const useAdminState = () => {
  return reactive(adminState)
}
